//
//  LoginController.m
//  i5db
//
//  Created by iMuse on 15-5-19.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "LoginController.h"
#import "AppCenter.h"
#import "AppDelegate.h"
#import "MyController.h"
#import "RegisterController.h"
@interface LoginController ()
{
    UITextField * userNameField;
    UITextField * passwordField;
    UILabel     * line;
    UIButton    * loginButton;
    UIImageView * imageView;
    UIButton    * forgButton;
    UIButton    * registerButton;
}

@end

@implementation LoginController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)updateViewConstraints
{
    [super updateViewConstraints];
    [imageView autoPinToTopLayoutGuideOfViewController:self withInset:80];
    [imageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [imageView autoSetDimensionsToSize:CGSizeMake(80, 80)];
    
    [userNameField autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:imageView withOffset:15];
    [userNameField autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [userNameField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [userNameField autoSetDimension:ALDimensionHeight toSize:45];
    
    [line autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:userNameField withOffset:0];
    [line autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [line autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [line autoSetDimension:ALDimensionHeight toSize:0.5];
    
    [passwordField autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:line withOffset:0];
    [passwordField autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [passwordField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [passwordField autoSetDimension:ALDimensionHeight toSize:45];
    
    [loginButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:passwordField withOffset:10];
    [loginButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [loginButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    [loginButton autoSetDimension:ALDimensionHeight toSize:40];
    
    [registerButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [registerButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    
    [forgButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [forgButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
   
   
   
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsUpdateConstraints];
    [self initView];
    
}

-(void) initView
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"login_bg.jpg"]];
    
    imageView = [UIImageView newAutoLayoutView];
    imageView.image = [UIImage imageNamed:@"shui.png"];
  
    [self.view addSubview:imageView];

    
    userNameField = [UITextField newAutoLayoutView];
    userNameField.textAlignment = NSTextAlignmentCenter;
    userNameField.placeholder = @"手机号/邮箱";
    userNameField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:userNameField];
    line = [UILabel newAutoLayoutView];
    line.backgroundColor = [AppCenter color:@"188,188,188,1"];
    [self.view addSubview:line];
    
    passwordField = [UITextField newAutoLayoutView];
    passwordField.secureTextEntry = YES;
    passwordField.placeholder = @"密码";
    passwordField.textAlignment = NSTextAlignmentCenter;
    passwordField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:passwordField];
    
    loginButton = [UIButton newAutoLayoutView];
    loginButton.layer.borderWidth = 1;
    loginButton.layer.cornerRadius = 4;
    loginButton.layer.borderColor = [AppCenter color:@"76,187,248,1"].CGColor;
    [loginButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    registerButton = [UIButton newAutoLayoutView];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [registerButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    [registerButton setTitle:@"新用户" forState:UIControlStateNormal];
    [registerButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [self.view addSubview:registerButton];
    
    forgButton = [UIButton newAutoLayoutView];
    forgButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [forgButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    [forgButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [self.view addSubview:forgButton];
}
-(void) login
{
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
	[appDelegate showTabBar];
    
    
}
-(void)registerButton:(id)sender
{
   
    RegisterController * controller = [[RegisterController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController setNavigationBarHidden:YES animated:TRUE];
    
    [super viewWillAppear:animated];
    
    
}
-(void)viewDidAppear:(BOOL)animated{
    //Calling this methods builds the intro and adds it to the screen. See below.
  
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
